package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.converter.MachinePartsBindConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.MachinePartsBindDTO;
import com.neusoft.neuiotms.dcam.fixed.model.MachinePartsBind;
import com.neusoft.neuiotms.dcam.fixed.repository.MachinePartsBindRepository;
import com.neusoft.neuiotms.dcam.fixed.service.MachinePartsBindService;

@Service
public class MachinePartsBindServiceImpl implements MachinePartsBindService {

    @Autowired
    private MachinePartsBindConverter partsBindConverter;
    @Autowired
    private MachinePartsBindRepository partsBindRepository;

    @Override
    public MachinePartsBindDTO create(final MachinePartsBindDTO dto) {
        final MachinePartsBind model = this.partsBindConverter.toModel(dto);
        this.partsBindRepository.save(model);
        return this.partsBindConverter.toDTO(model);
    }

    @Override
    public void saveBind(final Searchable bindable) {
        if ((!StringUtils.isEmpty(bindable.getStrValue("selectStr")))
                && (!StringUtils.isEmpty(bindable.getStrValue("bindCode")))
                && (!StringUtils.isEmpty(bindable.getStrValue("bindName")))) {
            final String bindCode = bindable.getStrValue("bindCode");
            final String selectStr = bindable.getStrValue("selectStr");
            final String[] selectionArray = selectStr.split("_");
            for (final String item : selectionArray) {
                final MachinePartsBindDTO dto = new MachinePartsBindDTO();
                final String partCode = item.split("-")[0];
                final String partName = item.split("-")[1];
                //判断机器配件是否绑定过
                final List<MachinePartsBind> invalidBind =
                        this.partsBindRepository.findByBindCodeAndPartsCodeAndStatus(bindCode, partCode, "1");
                //未绑定过，新增一条绑定记录
                if ((invalidBind != null) && (invalidBind.size() <= 0)) {
                    dto.setPartsCode(partCode);
                    dto.setPartsName(partName);
                    dto.setBindCode(bindCode);
                    dto.setBindName(bindable.getStrValue("bindName"));
                    dto.setStatus("1");
                    final MachinePartsBind model = this.partsBindConverter.toModel(dto);
                    this.partsBindRepository.save(model);
                } else { // 绑定过，更新绑定状态
                    final MachinePartsBind model = invalidBind.get(0);
                    model.setStatus("1");
                    this.partsBindRepository.save(model);
                }
            }
        }
    }

    @Override
    public void deleteBind(final Searchable bindable) {
        if ((!StringUtils.isEmpty(bindable.getStrValue("selectStr")))
                && (!StringUtils.isEmpty(bindable.getStrValue("bindCode")))
                && (!StringUtils.isEmpty(bindable.getStrValue("bindName")))) {
            final String bindCode = bindable.getStrValue("bindCode");
            final String selectStr = bindable.getStrValue("selectStr");
            final String[] selectionArray = selectStr.split("_");
            for (final String item : selectionArray) {
                final String partCode = item.split("-")[0];
                //获取绑定有效的机器配件
                final List<MachinePartsBind> invalidBind =
                        this.partsBindRepository.findByBindCodeAndPartsCodeAndStatus(bindCode, partCode, "1");
                if ((invalidBind != null) && (invalidBind.size() > 0)) {
                    final MachinePartsBind model = invalidBind.get(0);
                    model.setStatus("0");
                    this.partsBindRepository.save(model);
                }
            }
        }
    }

    @Override
    public MachinePartsBindDTO update(final Long id, final MachinePartsBindDTO dto) {
        MachinePartsBind model;
        try {
            model = this.partsBindRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        dto.setId(model.getId());
        model = this.partsBindConverter.toModel(dto);
        this.partsBindRepository.save(model);
        return this.partsBindConverter.toDTO(model);
    }

    @Override
    public List<MachinePartsBindDTO> findAll() {
        final List<MachinePartsBind> models = this.partsBindRepository.findAll();
        return this.partsBindConverter.toListDTO(models);
    }

    @Override
    public MachinePartsBindDTO details(final Long id) {
        final MachinePartsBind model;
        try {
            model = this.partsBindRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        return this.partsBindConverter.toDTO(model);
    }

    @Override
    public Page<MachinePartsBindDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<MachinePartsBind> result = this.partsBindRepository.search(pageable, searchable);
        return this.partsBindConverter.toPageDTO(result);
    }

}
